// pages/discover/news/news-list.js
const app = getApp()
import utils from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blogList: null,
    noMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  onShow(){
    this.setData({blogList: null, noMore: false})
    this.loadBlogs()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({blogList: null})
    this.loadBlogs()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.blogList==null || this.data.blogList.currentpage==this.data.blogList.totalpage){
      this.setData({noMore:true})
      return
    }
    let tmp = this.data.blogList
    tmp.currentpage=tmp.currentpage+1
    this.setData({blogList: tmp})
    this.loadNews()
  },

  //查询帖子信息
  loadBlogs: function(){
    let that = this
    let req = {serviceType: 1}
    if(this.data.blogList!=null){
      req.total=this.data.blogList.totalrecord,
      req.page=this.data.blogList.currentpage
    }
    utils.request(
      app.globalData.apiBaseUrl+'/forum/list',
      req
    ).then(res=>{
      wx.stopPullDownRefresh()
      let existList = that.data.blogList
        if(existList!=null){
          existList.records=existList.records.concat(res.data.records)
          that.setData({
            blogList: existList
          })
        }else{
          that.setData({
            blogList: res.data,
            totalrecords: res.data.totalrecords,
            page: res.data.page
          })
        }
    })
  },

  toBlogDetail: function(e){
    let bid = e.currentTarget.dataset.blogid
    console.log('跳转到详情页',bid)
    wx.navigateTo({
      url: '/pages/discovery/blog-detail?bid='+bid,
    })
  },

  toAddPage: function(e){
    wx.navigateTo({
      url: '/pages/discovery/publish',
    })
  }
})